package action

import (
	"fmt"
	"github.com/Ming0706/gorm/internal"
)

func Delete() {
	// 批量删除
	// 删除匹配的所有记录
	// delete from user where name like "%卢友明%";
	if err := internal.DB.Where("name like ?", "%卢友明%").Delete(&User{}).Error; err != nil {
		fmt.Printf("删除数据错误！\nError: %v\n", err)
	}
	// delete from user where name like "%卢友明%";
	if err := internal.DB.Delete(User{}, "name like ?", "%卢友名%").Error; err != nil {
		fmt.Printf("删除数据错误！\nError: %v\n", err)
	}

	// 物理删除
	// 定义需要删除的结构体，主键字段必须要有值
	user := User{Id: 6}
	// Unscoped 方法可以物理删除记录
	// delete from user where id = 1;
	internal.DB.Unscoped().Delete(&user)
}
